package com.ygqh.baby.controller.mobile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.ao.FocusType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.SalesPlatform;
import com.ygqh.baby.constant.RedisConstant;
import com.ygqh.baby.model.YgFocusModel;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.YgFileTemplateService;
import com.ygqh.baby.service.YgFocusService;
import com.ygqh.baby.service.YgPagesService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 焦点图
 *
 * @author ZhangSai
 */
@Controller
@RequestMapping("/mobile/focus")
public class YgFocusController {

    @Autowired
    private YgFocusService ygFocusService;
    @Autowired
    private YgFileTemplateService ygFileTemplateService;
    @Autowired
    private RedisDao redisDao;
    @Autowired
    private YgPagesService ygPagesService;

    @RequestMapping(value = "list")
    @ResponseBody
    public JSONPObject list(@RequestParam String callback, @RequestParam String... pageKey) {
        List<YgFocusModel> resultSet = ygFocusService.searchModelByCache(FocusType.Focus, pageKey);
        return new JSONPObject(callback, Message.success(resultSet));
    }


    @RequestMapping(value = "imglist")
    @ResponseBody
    public JSONPObject imglist(@RequestParam() FileType fileType, @RequestParam() String callback) {
        //List<YgFileTemplate> resultSet = ygFileTemplateService.find(fileType);

		/*Map<String,Object> map=new HashMap<String,Object>();
		for(YgFileTemplate t:resultSet){
			String areaName=t.getAreaName();
			List<YgFileTemplate> areaList=new ArrayList<YgFileTemplate>();
			
			for(YgFileTemplate tt:resultSet)
			{
				if(tt.getAreaName().equals(areaName)){
					areaList.add(tt);
				}
				if(tt.getName().contains("banner"))
					map.put(tt.getName(),tt);
			}
			map.put(areaName, areaList);
				
			
		}*/
		/*String s=redisDao.get("newMall");
		return new JSONPObject(callback, Message.success(s));*/
        String result = ygFileTemplateService.findValidMallImgPath(fileType);
        return new JSONPObject(callback, Message.success(result));
    }

    /***
     * 获取页面类型为广告的数据
     * @param callback
     * @return
     */
    @RequestMapping(value = "getAdProduct")
    @ResponseBody
    public JSONPObject getAdProduct(@RequestParam() String callback) {
        String content = ygPagesService.findAdvertisingPage();
        return new JSONPObject(callback, Message.success(content));
    }

    /**
     * 分类页
     *
     * @return
     */
    @RequestMapping("findCategoryImg")
    @ResponseBody
    public JSONPObject findCategoryImg(String callback,FileType fileType) {
    	if(fileType==null)
    		fileType=FileType.Category;
        return new JSONPObject(callback, Message.success(ygFileTemplateService.findCategoryImg(fileType)));
    }
    /**
     * 获取首页tab 标签内容
     *
     * @return
     */
    @RequestMapping("findHomeTab")
    @ResponseBody
    public JSONPObject findHomeTab(String callback,SalesPlatform salesPlatform) {
    	String key = String.format(RedisConstant.MALL_INDEX_TAB_KEY,salesPlatform.name());		
        return new JSONPObject(callback, Message.success(redisDao.get(key)));
    }

}
